package com.jgp.glmeeting.mobile.controller;

import com.jgp.glmeeting.mobile.commons.BaseResponse;
import com.jgp.glmeeting.mobile.commons.ErrorMsg;
import com.jgp.glmeeting.model.Arrangement;
import com.jgp.glmeeting.model.Meeting;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.repository.ArrangementRepository;
import com.jgp.glmeeting.service.MeetingService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.query.Operator;
import com.jgp.sys.common.query.QueryFilterList;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会议查阅模块的控制器
 */
@RestController
@RequestMapping("mobile/MobileMeetingController")
public class MobileMeetingController {

    private final Logger logger = LoggerFactory.getLogger(MobileMeetingController.class);

    @Autowired
    private MeetingService meetingService;
    @Autowired
    private ParticipantsService participantsService;
    @Autowired
    private ArrangementRepository arrangementRepository;

    /**
     * 个人会议信息查阅
     * @param participantsId
     * @param group
     * @return
     */
    @RequestMapping("queryMeetingInfo")
    public BaseResponse queryMeetingInfo(String participantsId, String group) {
        if(StringUtils.isBlank(participantsId) || StringUtils.isBlank(group)){
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            Map<String,Object> responseObj = new HashMap<>();
            //查询此人参加的会议
            List<Meeting> meetingList = meetingService.queryListByParticipantsId(participantsId);
            if(CollectionUtils.isEmpty(meetingList)){
                logger.error(ErrorMsg.MEETING_NOT_FOUND.getErrorMsg() +":: participantsId={}",participantsId);
                return new BaseResponse(ErrorMsg.MEETING_NOT_FOUND.getCode(), ErrorMsg.MEETING_NOT_FOUND.getErrorMsg());
            }
            Meeting meeting  = meetingList.get(0);
            responseObj.put("meeting", meeting);

            //查询此人的同组人员
            List<Participants> otherParticipants = participantsService.queryListByGroupAndId(participantsId, group);
            responseObj.put("otherParticipants", otherParticipants);

            //查询此人的安排信息
            List<Arrangement> arrangementList = arrangementRepository.read(new QueryFilterList().addFilter("participantsId", Operator.eq, participantsId).addFilter("planDate", Operator.eq, LocalDate.now()));
            if(CollectionUtils.isNotEmpty(arrangementList)){
                responseObj.put("arrangement", arrangementList.get(0));
            }else {
                Arrangement arrangement = new Arrangement();
                String text = "未安排";
                arrangement.setBusNo(text);
                arrangement.setDiningArrange(text);
                arrangement.setRoomNo(text);
                arrangement.setSiteNo(text);
                responseObj.put("arrangement", arrangement);
            }

            BaseResponse baseResponse =  new BaseResponse(200);
            baseResponse.setObj(responseObj);
            return baseResponse;
        }catch (Exception e){
            logger.error("个人会议信息查阅异常, 参数participantsId={}, group={}",e, participantsId, group);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }
    }


    /**
     * 反正就一个会议这里简单查一下
     */
    @RequestMapping("queryOneMeetingInfo")
    public BaseResponse queryOneMeetingInfo(Meeting m){
        Meeting meeting=meetingService.queryAll(m).get(0);
        BaseResponse baseResponse =  new BaseResponse(200);
        baseResponse.setObj(meeting);
        return baseResponse;
    }



}
